package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsStadium;
import com.bwie.pojo.Image;
import com.bwie.pojo.Stadium;
import com.bwie.service.ImageService;
import com.bwie.service.StadiumService;
import org.bouncycastle.cms.PasswordRecipient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TaskStadium {
     @Autowired
     private StadiumService stadiumService;

     @Autowired
     private ImageService imageService;

     @Autowired
     private ElasticsearchRestTemplate elasticsearchRestTemplate;

     @Scheduled(cron = "0/30 * * * * ?")
     public void task(){
          System.out.println("开启同步");
          List<Stadium> stadiumList = stadiumService.stadiumList();
          List<Image> imageList = imageService.list();
          for (Stadium stadium : stadiumList) {
               ArrayList<Image> list = new ArrayList<>();
               for (Image image : imageList) {
                    if (image.getStadiumId() == stadium.getId()){
                         list.add(image);
                    }
               }
               stadium.setImageList(list);
               EsStadium esStadium = BeanUtil.toBean(stadium, EsStadium.class);
               elasticsearchRestTemplate.save(esStadium);
          }
          System.out.println("关闭同步");
     }
}
